package com.ruizhong.centercontroll.util;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class RemoteControlUtil {
    public static void Recalculate(MouseInfo info) {
        double textureHeight = info.getFitHeight();
        double textureWidth = info.getFitWidth();
        getRealFitWidthAndRealFitHeight(info);
//       计算realX和RealY
        double realX, realY;
        realX = info.getX() - ((textureWidth - info.getFitWidth()) / 2);
        realY = info.getY() - ((textureHeight - info.getFitHeight()) / 2);
        if (realX < 0 || realY < 0) {
            return;
        }
        info.setX(realX);
        info.setY(realY);

    }

    private static void getRealFitWidthAndRealFitHeight(MouseInfo info) {
        double textureHeight = info.getFitHeight();
        double textureWidth = info.getFitWidth();
        double R = Server.remoteImageWidth / Server.remoteImageHeight;
        double realFitWidth, realFitHeight = 0.0;
        realFitHeight = textureHeight;
        realFitWidth = realFitHeight * R;
        if (realFitWidth > textureWidth) {
            realFitWidth = textureWidth;
            realFitHeight = realFitWidth / R;
        }
        info.setFitHeight(realFitHeight);
        info.setFitWidth(realFitWidth);
    }

    public static void mousePress(MouseInfo info) {
        int x = (int) (info.getX() / info.getFitWidth() * Server.remoteImageWidth);
        int y = (int) (info.getY() / info.getFitHeight() * Server.remoteImageHeight);
        for (int i = 0; i < 10; i++)
            ControllerUtil.robot.mouseMove(x, y);
        ControllerUtil.robot.mousePress(16);
    }

    public static void mouseRelease(MouseInfo mouseInfo) {
        ControllerUtil.robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    }

    public static void clickRight() {
        ControllerUtil.robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
// 模拟鼠标右键按下
        ControllerUtil.robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);

        // 模拟鼠标右键松开
        ControllerUtil.robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
    }

    public static void inputKeyBoard(String text) {

        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        // 将文本设置到剪贴板
        StringSelection stringSelection = new StringSelection(text);
        clipboard.setContents(stringSelection, null);

        // 使用 Robot 模拟粘贴操作
        ControllerUtil.robot.keyPress(KeyEvent.VK_CONTROL); // 或 KeyEvent.VK_META 用于 Mac
        ControllerUtil.robot.keyPress(KeyEvent.VK_V);
        ControllerUtil.robot.keyRelease(KeyEvent.VK_V);
        ControllerUtil.robot.keyRelease(KeyEvent.VK_CONTROL); // 或 KeyEvent.VK_META 用于 Mac

    }

    public static void inputKeyBoardEnter() {
        ControllerUtil.robot.keyPress(KeyEvent.VK_ENTER);
        ControllerUtil.robot.keyRelease(KeyEvent.VK_ENTER);
    }

    public static void inputKeyBoardBackspace() {
        ControllerUtil.robot.keyPress(KeyEvent.VK_BACK_SPACE);
        ControllerUtil.robot.keyRelease(KeyEvent.VK_BACK_SPACE);
    }
}
